//
//  WEXComposeToolBar.swift
//  microBlog
//
//  Created by apple on 16/4/26.
//  Copyright © 2016年 Hill. All rights reserved.
//

import UIKit

enum WEXComposeToolBarButtonType: Int {
    
    case Picture = 0 ,Mention, Trend, Emoticon, Add
    
    
}

class WEXComposeToolBar: UIStackView {

    
    
    
    // MARK: - 定义变量
    var childButtonClosure: ((type: WEXComposeToolBarButtonType)->())?
    
    var isSystemKeyBoard: Bool = true {
        
        didSet{
            
            let button = self.viewWithTag(WEXComposeToolBarButtonType.Emoticon.rawValue) as? UIButton
            
            var name = "compose_keyboardbutton_background"
//            如果是系统键盘
            if isSystemKeyBoard {
//                图标显示为笑脸
                name = "compose_emoticonbutton_background"
                
            }
            
            button?.setImage(UIImage(named: "\(name)__highlighted"), forState: UIControlState.Highlighted)
            button?.setImage(UIImage(named: name), forState: UIControlState.Normal)
        }
        
        
        
    }
    
    // MARK: - 初始化方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
//        设置内部内容存放模式
        distribution = .FillEqually
        
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        
        addChildButton("compose_toolbar_picture",type: .Picture)
        addChildButton("compose_mentionbutton_background",type: .Mention)
        addChildButton("compose_trendbutton_background",type: .Trend)
        addChildButton("compose_emoticonbutton_background",type: .Emoticon)
        addChildButton("compose_add_background",type: .Add)
    
    }
    
    // 通过图片名称添加子控件
    private func addChildButton(imgName: String,type: WEXComposeToolBarButtonType) {
        
        let button = UIButton()
        
        button.addTarget(self, action: "childClickButton:", forControlEvents: UIControlEvents.TouchUpInside)
        
//        设置标识按钮的类型
        button.tag = type.rawValue
        
        button.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imgName)_highlighted"), forState: UIControlState.Highlighted)
        
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        
        addArrangedSubview(button)
    }
    
//    回调方法
    func childClickButton(button: UIButton) {
        
        
        childButtonClosure?(type: WEXComposeToolBarButtonType(rawValue: button.tag)!)
        
    }
    
}
